package com.sls.core.util;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 
 * @description IP地址所属省份解析<br>
 *              IP数据库来源：纯真IP库
 * @author Wangxuegang
 * @date 2013-4-22
 */
public class IPAddressResolution {

	private static Logger log = Logger.getLogger(IPAddressResolution.class);

	/**
	 * 获取IP地址所属的省份<br>
	 * IP纯真库解析
	 * @param ip
	 * @return
	 */
	public static String parseIPAddressOfChunzhen(String ip) {
		String url = "http://ip.itlearner.com/?" + "ip=" + ip;
		String str44 = null;
		try {
			// url处理
			Document doc = Jsoup.connect(url).get();
			Elements links = doc.select("meta"); // 带有href属性的a元素
			Element link = links.last();
			String str1 = link.toString();
			int index0 = str1.indexOf("具体位置");
			String index0Str = str1.substring(index0 + 5, str1.length());

			int index00 = index0Str.indexOf("移动");
			int index01 = index0Str.indexOf("铁通");

			if ((index00 != -1) || (index01 != -1)) {
				String index0String = str1.substring(index0 + 5, index0 + 7);
				str44 = index0String;
			} else {
				int index1 = str1.indexOf("域");
				int index2 = index1 + 2;
				String str3 = str1.substring(index2, str1.length());
				int index3 = str3.indexOf("省");
				if (index3 != -1) {
					String str4 = str3.substring(0, index3);
					return str4;
				} else {
					int index33 = str3.indexOf("市");
					str44 = str3.substring(0, index33);

					/**
					 * 自治区（广西、宁夏、内蒙古、新疆、西藏）特殊处理
					 */
					int guangxiIndex = str44.indexOf("广西");
					if (guangxiIndex != -1) {
						str44 = str44.substring(0, 2);
					}
				}
			}
			return str44;
		} catch (Exception e) {
			log.error(e);
		}

		return null;
	}

	/**
	 * 获取IP地址所属的省份<br>
	 * IP138网站解析
	 * @param ip
	 * @return
	 */
	public static String parseIPAddressOfIP138(String ip) {
		String url = "http://www.ip138.com/ips1388.asp?" + "ip=" + ip
				+ "&action=2";
		String result = null;
		try {
			// url处理
			Document doc = Jsoup.connect(url).get();
			Elements links = doc.select("li"); // 带有href属性的a元素
			Element link = links.first();
			String str1 = link.toString();

			int index00 = str1.indexOf("移动");
			int index01 = str1.indexOf("铁通");
			int index02 = str1.indexOf("联通");
			if ((index00 != -1)) {
				result = str1.substring(index00, index00 + 2);
			} else if (index01 != -1) {
				result = str1.substring(index01, index01 + 2);
			} else if (index02 != -1) {
				result = str1.substring(index02, index02 + 2);
			} else {
				/**
				 * 省份
				 */
				int index = str1.indexOf("省");
				if (index != -1) {
					result = str1.substring(10, index);
				} else {
					/**
					 * 自治区（广西、宁夏、内蒙古、新疆、西藏）特殊处理
					 */
					int guangxiIndex = str1.indexOf("广西");
					if (guangxiIndex != -1) {
						result = str1.substring(10, guangxiIndex + 2);
					} else {
						/**
						 * 直辖市
						 */
						int index0 = str1.indexOf("市");
						result = str1.substring(10, index0);
					}
				}
			}
		} catch (Exception e) {
			log.error(e);
		}
		return result;
	}

	/**
	 * @param args
	 */
	// public static void main(String[] args) {
	// String ip = "60.12.190.58";
	// String res = parseIPAddress(ip);
	// System.out.println(res);
	// }

}
